﻿namespace WMS6.Web.Billing
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BillingServiceRef;
    using WMS6.Web.UserControls;

    public class BillingOrderList : BasePage
    {
        protected Button btnAction;
        protected Button btnClosed;
        protected Button btnExport;
        protected Button btnList;
        protected Button btnNewSearch;
        protected Button btnReSearch;
        protected HtmlForm F9221;
        protected GridView gvBillingOrder;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnRtnValue;
        protected Label lblBillingOrderList;
        protected Label lblMsg;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        private void BindData2BillingOrderEntityList(BillingOrderQueryEntity billingOrderQueryEntity)
        {
            this.PagerFooter.CurrentPage = int.Parse(billingOrderQueryEntity.CurrentPage);
            DataSet ds = new DataSet();
            ds = BillingServiceClientProxy.GetInstance().GetBillingOrderByQueryList(billingOrderQueryEntity);
            base.BindData2GridView(this.gvBillingOrder, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnClosed_Click(object sender, EventArgs e)
        {
            this.lblMsg.Text = string.Empty;
            try
            {
                int i;
                List<string> docIDList = new List<string>();
                List<string> docTypeList = new List<string>();
                if (base.GetSysSettingValueByID("OrdersCloseNeedAuditInvPallet").ToUpper() != "Y")
                {
                    for (i = 0; i < this.gvBillingOrder.Rows.Count; i++)
                    {
                        if (((CheckBox) this.gvBillingOrder.Rows[i].FindControl("chbSelect")).Checked)
                        {
                            docIDList.Add(this.gvBillingOrder.DataKeys[i]["DOC_ID"].ToString());
                            docTypeList.Add(this.gvBillingOrder.DataKeys[i]["DOC_TYPE"].ToString());
                        }
                    }
                    if ((docIDList == null) || (docIDList.Count == 0))
                    {
                        this.lblMsg.Text = base.GetGlobalMsgResource("F9220_002");
                        return;
                    }
                }
                else
                {
                    for (i = 0; i < this.gvBillingOrder.Rows.Count; i++)
                    {
                        if (((CheckBox) this.gvBillingOrder.Rows[i].FindControl("chbSelect")).Checked && (this.gvBillingOrder.DataKeys[i]["IS_PALLET_AUDIT"].ToString().ToUpper() == "Y"))
                        {
                            docIDList.Add(this.gvBillingOrder.DataKeys[i]["DOC_ID"].ToString());
                            docTypeList.Add(this.gvBillingOrder.DataKeys[i]["DOC_TYPE"].ToString());
                        }
                    }
                    if ((docIDList == null) || (docIDList.Count == 0))
                    {
                        this.lblMsg.Text = "系统设置:只能选择箱托录入已审核的单据进行关闭操作!";
                        return;
                    }
                }
                string result = BillingServiceClientProxy.GetInstance().GenerateBillingCredence(docIDList, docTypeList);
                if (string.IsNullOrEmpty(result))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F9220_003");
                }
                else
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F9220_004") + result;
                }
                BillingOrderQueryEntity billingOrderQueryEntity = this.GetBillingOrderQueryEntity();
                this.BindData2BillingOrderEntityList(billingOrderQueryEntity);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "BillingOrderListPage", "<script language='javascript'>$.unblockUI();</script>", false);
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            BillingOrderQueryEntity billingOrderQueryEntity = this.GetBillingOrderQueryEntity();
            billingOrderQueryEntity.IsGetAll = true;
            this.hdnActionID.Value = "Y";
            string currentPageIndex = billingOrderQueryEntity.CurrentPage;
            billingOrderQueryEntity.CurrentPage = "0";
            this.gvBillingOrder.Columns[1].Visible = false;
            this.gvBillingOrder.AllowSorting = false;
            this.BindData2BillingOrderEntityList(billingOrderQueryEntity);
            this.gvBillingOrder.ShowFooter = false;
            this.hdnActionID.Value = "";
            billingOrderQueryEntity.IsGetAll = false;
            billingOrderQueryEntity.CurrentPage = currentPageIndex;
            this.SetBillingOrderQueryEntity(billingOrderQueryEntity);
            base.Export2Excel("BillingOrder.xls", this.gvBillingOrder);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            BillingOrderQueryEntity billingOrderQueryEntity = this.GetBillingOrderQueryEntity();
            billingOrderQueryEntity.CurrentPage = "0";
            this.BindData2BillingOrderEntityList(billingOrderQueryEntity);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            BillingOrderQueryEntity billingOrderQueryEntity = this.GetBillingOrderQueryEntity();
            billingOrderQueryEntity.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2BillingOrderEntityList(billingOrderQueryEntity);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            BillingOrderQueryEntity billingOrderQueryEntity = this.GetBillingOrderQueryEntity();
            billingOrderQueryEntity.DocID = "";
            billingOrderQueryEntity.DocType = "";
            billingOrderQueryEntity.OwnerID = "";
            billingOrderQueryEntity.CreatedBy = "";
            billingOrderQueryEntity.CreatedDate = "";
            billingOrderQueryEntity.IsPalletInput = "";
            billingOrderQueryEntity.IsPalletAudit = "";
            this.BindData2BillingOrderEntityList(billingOrderQueryEntity);
        }

        protected void btnNewSearch_Click(object sender, EventArgs e)
        {
            base.Server.Transfer("BillingOrderQuery.aspx");
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            BillingOrderQueryEntity billingOrderQueryEntity = this.GetBillingOrderQueryEntity();
            if (int.Parse(billingOrderQueryEntity.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                billingOrderQueryEntity.CurrentPage = (int.Parse(billingOrderQueryEntity.CurrentPage) + 1).ToString();
                this.BindData2BillingOrderEntityList(billingOrderQueryEntity);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            BillingOrderQueryEntity billingOrderQueryEntity = this.GetBillingOrderQueryEntity();
            if (int.Parse(billingOrderQueryEntity.CurrentPage) > 0)
            {
                billingOrderQueryEntity.CurrentPage = (int.Parse(billingOrderQueryEntity.CurrentPage) - 1).ToString();
                this.BindData2BillingOrderEntityList(billingOrderQueryEntity);
            }
        }

        private BillingOrderQueryEntity GetBillingOrderQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as BillingOrderQueryEntity);
            }
            return (base.PageQueryEntitySession as BillingOrderQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                BillingOrderQueryEntity billingOrderQueryEntity = this.GetBillingOrderQueryEntity();
                billingOrderQueryEntity.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2BillingOrderEntityList(billingOrderQueryEntity);
            }
        }

        protected void gvBillingOrder_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F9221", this.gvBillingOrder);
        }

        protected void gvBillingOrder_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
            }
        }

        protected void gvBillingOrder_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            string strValue = base.UrlReplace(this.gvBillingOrder.DataKeys[e.NewSelectedIndex]["RATE_ID"].ToString());
            if (!string.IsNullOrEmpty(strValue))
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'> window.returnValue ='" + strValue + "';  window.close();</script>", false);
            }
        }

        protected void gvBillingOrder_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                BillingOrderQueryEntity billingOrderQueryEntity = this.GetBillingOrderQueryEntity();
                billingOrderQueryEntity.SortField = e.SortExpression;
                if (billingOrderQueryEntity.SortDirection == "ASC")
                {
                    billingOrderQueryEntity.SortDirection = "DESC";
                }
                else if (billingOrderQueryEntity.SortDirection == "DESC")
                {
                    billingOrderQueryEntity.SortDirection = "ASC";
                }
                this.BindData2BillingOrderEntityList(billingOrderQueryEntity);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            BillingOrderQueryEntity billingOrderQueryEntity = new BillingOrderQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    billingOrderQueryEntity.DocID = this.PreviousPage.DocID;
                    billingOrderQueryEntity.DocType = this.PreviousPage.DocType;
                    billingOrderQueryEntity.OwnerID = this.PreviousPage.OwnerID;
                    billingOrderQueryEntity.CreatedBy = this.PreviousPage.CreatedBy;
                    billingOrderQueryEntity.CreatedDate = this.PreviousPage.CreatedDate;
                    billingOrderQueryEntity.IsPalletInput = this.PreviousPage.IsPalletInput;
                    billingOrderQueryEntity.IsPalletAudit = this.PreviousPage.IsPalletAudit;
                }
                else
                {
                    billingOrderQueryEntity.DocID = "";
                    billingOrderQueryEntity.DocType = "";
                    billingOrderQueryEntity.OwnerID = "";
                    billingOrderQueryEntity.CreatedBy = "";
                    billingOrderQueryEntity.CreatedDate = "";
                    billingOrderQueryEntity.IsPalletInput = "";
                    billingOrderQueryEntity.IsPalletAudit = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            billingOrderQueryEntity.IsGetAll = false;
            billingOrderQueryEntity.CurrentPage = "0";
            billingOrderQueryEntity.PageSize = this.gvBillingOrder.PageSize.ToString();
            billingOrderQueryEntity.SortField = "ORDERS.DOC_ID";
            billingOrderQueryEntity.SortDirection = "ASC";
            this.PagerFooter.PageSize = this.gvBillingOrder.PageSize;
            if (!string.IsNullOrEmpty(base.Request.QueryString["action"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["action"].ToString();
                this.gvBillingOrder.Columns[1].Visible = false;
                this.gvBillingOrder.Columns[2].Visible = true;
                this.btnClosed.Visible = false;
                this.btnReSearch.Visible = false;
                this.btnExport.Visible = false;
            }
            else if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            this.SetBillingOrderQueryEntity(billingOrderQueryEntity);
            this.BindData2BillingOrderEntityList(billingOrderQueryEntity);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.lblMsg.Text = "";
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetBillingOrderQueryEntity(BillingOrderQueryEntity billingOrderQueryEntity)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = billingOrderQueryEntity;
            }
            else
            {
                base.PageQueryEntitySession = billingOrderQueryEntity;
            }
        }

        public BillingOrderQuery PreviousPage
        {
            get
            {
                return (BillingOrderQuery) base.PreviousPage;
            }
        }
    }
}

